home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (c) 1988 Bellcore
- * All Rights Reserved
- * Permission is granted to copy or use this program, EXCEPT that it
- * may not be sold for profit, the copyright notice must be reproduced
- * on copies, and credit should be given to Bellcore where it is due.
- * BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
- */
- /* $Header: bitmapread.c,v 1.1 89/03/17 08:20:52 sau Exp $
- $Source: /m1/mgr.new/src/RCS/bitmapread.c,v $
- */
- static char RCSid_[] = "$Source: /m1/mgr.new/src/RCS/bitmapread.c,v $$Revision: 1.1 $";
-
- #include "dump.h"
- #include "bitmap.h"
- #include <stdio.h>
-
-
- /*
- Read a bitmap.
- Given an open FILE pointer to an file containing a bitmap,
- read the header, malloc() a bitmap, and return the pointer to the
- bitmap.
- */
- BITMAP *
- bitmapread( fp )
- register FILE *fp;
- {
- BITMAP *bp = 0;
- register char *datap;
- int h, w, d; /* height, width, depth of bitmap */
- int sizefile1; /* the size of 1 line of the bitmap
- as stored in a file, in bytes */
- int sizemem1; /* the size of 1 line of the bitmap
- as stored in memory, in bytes */
- long size1diff = 0; /* if the file padding is greater than
- the memory padding, the difference in
- bytes */
-
- if( bitmaphead( fp, &w , &h, &d, &sizefile1 ) ) {
- sizemem1 = BIT_Size(w, 1, d);
- if( sizefile1 > sizemem1 ) {
- size1diff = sizefile1 - sizemem1;
- sizefile1 = sizemem1;
- }
- if( !(bp = bit_alloc(w, h, NULL_DATA, d)) )
- return 0;
- datap = (char *)BIT_DATA(bp);
-
- /* The bytes of the bitmap data in the file may have
- different alignments than the bitmap data in memory.
- We read one line at a time in such a way as to get
- the memory alignment needed.
- */
- while( h-- > 0 ) {
- if( fread( datap, sizefile1, 1, fp ) != 1 ) {
- free( (char *)bp );
- return 0;
- }
- if( size1diff ) {
- fseek( fp, size1diff, 1 );
- }
- datap += sizemem1;
- }
- }
- return bp;
- }
-
-
- /*
- Write a bitmap.
- Given an open FILE pointer to an file and a pointer to a bitmap,
- write the header and the bitmap. Return 0 on failure, positive on
- success.
- */
- int
- bitmapwrite( fp, bp, flag )
- register FILE *fp;
- BITMAP *bp;
- int flag; /* 1->8 bit (new), 0->16 bit (old) */
-
- {
- register char *datap;
- register int w, h, d;
- struct b_header head;
- struct old_b_header old_head;
-
- int sizefile1; /* the size of 1 line of the bitmap
- as stored in a file, in bytes */
- int sizemem1; /* the size of 1 line of the bitmap
- as stored in memory, in bytes */
-
- w = BIT_WIDE(bp);
- h = BIT_HIGH(bp);
- d = BIT_DEPTH(bp);
- switch(flag) {
- case NEW_BHDR:
- B_PUTHDR8( &head, w, h, d );
- sizefile1 = B_SIZE8(w, 1, d);
- if( fwrite( (char *)&head, sizeof head, 1, fp ) != 1 )
- return 0;
- break;
- case OLD_BHDR:
- B_PUTOLDHDR( &old_head, w, h );
- sizefile1 = B_SIZE16(w, 1, d);
- if( fwrite( (char *)&old_head, sizeof old_head, 1, fp ) != 1 )
- return 0;
- break;
- }
-
- sizemem1 = BIT_Size(w, 1, d);
- datap = (char *)BIT_DATA(bp);
- while( h-- > 0 ) {
- if( fwrite( datap, sizefile1, 1, fp ) != 1 )
- return 0;
- datap += sizemem1;
- }
- return(1);
- }
-